//
//  MSHomeDetailView.m
//  MSJD
//
//  Created by xiangbo on 2018/9/22.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSHomeDetailView.h"
#import "MSTemplateViewCell.h"
#import "MSTemplateCellUtil.h"

@interface MSHomeDetailView ()<UITableViewDelegate,UITableViewDataSource, MSHomeHeadViewDelegate>

@property (nonatomic, strong)NSArray *dataArray;

@end

@implementation MSHomeDetailView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self addSubview:self.headView];
        [self addSubview:self.tableview];
        
        [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self);
            make.height.mas_equalTo(167);
        }];
        
        [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.headView.mas_bottom);
            make.left.right.bottom.equalTo(self);
        }];
    }
    return self;
}

#pragma mark -UITableViewDelegate and DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MSFoodData *data = self.dataArray[indexPath.row];
    MSTemplateViewCell *cell = [tableView dequeueReusableCellWithIdentifier:data.showType forIndexPath:indexPath];
    [cell setupTemplateData:data];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     MSFoodData *data = self.dataArray[indexPath.row];
    if ([data.showType isEqualToString:@"d"]) {
        return [MSTemplateCellUtil getTemplateCellSize:data.showType].height + 44;
    }
    return  [MSTemplateCellUtil getTemplateHeight:data.showType];
}

#pragma mark - MSHomeHeadViewDelegate
/** 跳转到搜索界面 */
- (void)showSearchViewController {
    if (self.SearchButtonClickBlock) {
        self.SearchButtonClickBlock();
    }
}

#pragma mark - Property

- (MSHomeHeadView *)headView {
    if (!_headView) {
        _headView = [[MSHomeHeadView alloc] initWithFrame:CGRectZero headType:MSHeadViewTypeFood];
        _headView.customDelegate = self;
    }
    return _headView;
}
- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableview registerClass:[MSTemplateViewCell class] forCellReuseIdentifier:NSStringFromClass([MSTemplateViewCell class])];
        
        for (NSString *identifier in MSIdentifierDic.allKeys) {
            [_tableview registerClass:NSClassFromString(MSIdentifierDic[identifier]) forCellReuseIdentifier:identifier];
        }
    }
    return _tableview;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSString *key in [[MSIdentifierDic allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
            MSFoodData *data = [[MSFoodData alloc] init];
            data.showType = key;
            [tempArray addObject:data];
        }
        
        MSFoodData *data = [[MSFoodData alloc] init];
        data.showType = @"d";
        [tempArray insertObject:data atIndex:0];
        
        _dataArray = tempArray.copy;
    }
    return _dataArray;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
